import { DeviceManager } from './../../../../../common/mpj-deviceCenter/src/deviceManager';
import { PrintService } from './../../../../../service/print.service';
import { PrintComponentService } from './../printcontainer.component';
import { PrinterManager } from './../../../../../common/devices/printer/printerManager';
import { Table, Order } from './../../../../../model/clerkDataContext';
import { Component, OnInit, Output, EventEmitter, ChangeDetectorRef } from '@angular/core';

export class ReceiptPrintService {
    static current: ReceiptPrintService = new ReceiptPrintService();

    private salesReceipt: SalesReceiptComponent;

    regist(salesReceipt: SalesReceiptComponent) {
        this.salesReceipt = salesReceipt;
    }

    clearData() {
        this.salesReceipt.preOrder = <any>{};
        this.salesReceipt.store = <any>{};
    }

    bindData(data: {
        store;
        preOrder;
        token;
        printers;
    }) {
        this.salesReceipt.store = data.store;
        this.salesReceipt.preOrder = data.preOrder;
        this.salesReceipt.token = data.token;
        this.salesReceipt.printers = data.printers;
        if (data.printers && data.printers.length) {
            this.salesReceipt.currentPrinter = data.printers[0];
        }
    }
}

@Component({
    selector: 'sales-receipt',
    templateUrl: "./salesreceipt.component.html",
    styleUrls: ["./../../printer.component.scss"]
})
export class SalesReceiptComponent implements OnInit {
    constructor(
        private printSvr: PrintService,
        private ref: ChangeDetectorRef
    ) {
        ReceiptPrintService.current.regist(this);
    }
    currencySymbol = "¥";
    title = "记账单";
    store: any = {};
    preOrder: Order = <any>{};
    token: string;
    printers: any = [];
    currentPrinter: any = {};
    printerManager;
    adsServer;
    @Output() createPrintTask: EventEmitter<any> = new EventEmitter<any>();

    ngOnInit() {
        this.printerManager = DeviceManager.Current.getManagerByType("printerManager");
        if (this.store.setting && this.store.setting.setup) {
            this.currencySymbol = this.store.setting.setup.symbol;
        }
    }

    dataBindFinish() {
        this.printTask();
    }

    printTask() {
        let that = this;
        let html = document.querySelector(".print-receipt");
        let tempClassName = "";
        this.adsServer = this.printSvr.adsServer;
        if (!this.adsServer) {
            console.warn("ads服务未配置!");
            return;
        }
        if (this.printers && this.printers.length) {
            let tasks = [];
            for (let i = 0; i < this.printers.length; i++) {
                this.currentPrinter = this.printers[i];
                let outHtml = html.outerHTML.replace("print-receipt", "print-receipt " + this.currentPrinter.paperSize);
                let printParams = {
                    printMode: "http",
                    printTemplate: outHtml,
                    url: this.adsServer.url + "/printer/print/html",
                    mode: "text",
                    deviceInfo: this.printers[i].deviceInfo,
                    printId: "",
                    type: "wifi",
                    storeId: this.store.id,
                    token: this.token,
                    directives: {
                        "auto-cut": this.printers[i].autoCut,
                        "print-mode": "text",
                        "open-cash-drawer": false,
                        "paper-scroll-rows": this.printers[i].paperScrollRows,
                        "instruction-set": this.printers[i].instructionSet
                    }
                }
                tasks.push(printParams)
            }
            that.createPrintTask.emit(tasks);
        }
        this.ref.detectChanges();
    }
}